/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package primephysique;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author AP
 */
public class ActiveTrainee extends javax.swing.JPanel {

    /**
     * Creates new form ActiveTrainee
     */
    public ActiveTrainee() {
        initComponents();
        //add date for combobox
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String query= "SELECT BranchName FROM Branch";
            rs=st.executeQuery(query);
            Vector brData=new Vector();
           
            brData.add("All");
            while(rs.next()){
                brData.add(rs.getString(1));
            }
            cbbBranch.setModel(new DefaultComboBoxModel(brData));
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
private void showResult() {
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
           
            int b=cbbBranch.getSelectedIndex();
            //query= "SELECT BranchID FROM Branch WHERE BranchName= cbbBranch.getSlectedItem.toString.trim()";
            // get range of infomation
             String query="";
            if(b==0) query = "SELECT count(*) as nbot FROM Trainee WHERE Active= 'True'";
            else query = "SELECT count(*) as nbot FROM Trainee WHERE Active= 'True'"
                    + " AND ClassID IN (SELECT ClassID From Class WHERE BranchID='"+b+"')";
           
            
            rs = st.executeQuery(query);
            // set text to label
            rs.next();
            lbCount.setText(rs.getString(1));
           
            
            con.close();


        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scpTop = new javax.swing.JPanel();
        cbbBranch = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        lbBranch = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lbCount = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        cbbBranch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Ha noi", "Ho chi minh", "Da nang" }));

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        lbBranch.setText("Branch");

        jLabel1.setText("Number Trainee");

        javax.swing.GroupLayout scpTopLayout = new javax.swing.GroupLayout(scpTop);
        scpTop.setLayout(scpTopLayout);
        scpTopLayout.setHorizontalGroup(
            scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scpTopLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(lbBranch))
                .addGap(28, 28, 28)
                .addGroup(scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(scpTopLayout.createSequentialGroup()
                        .addComponent(cbbBranch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSearch))
                    .addComponent(lbCount))
                .addContainerGap(126, Short.MAX_VALUE))
        );
        scpTopLayout.setVerticalGroup(
            scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scpTopLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbbBranch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnSearch))
                    .addComponent(lbBranch))
                .addGap(18, 18, 18)
                .addGroup(scpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbCount))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        add(scpTop, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // TODO add your handling code here:
         showResult();
    }//GEN-LAST:event_btnSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbbBranch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbBranch;
    private javax.swing.JLabel lbCount;
    private javax.swing.JPanel scpTop;
    // End of variables declaration//GEN-END:variables
    private ConnectDB cdb = null;
    private Connection con;
    private Statement st;
    private ResultSet rs;
}
